package com.axzhengxin.platform.business.service.impl;

import java.text.ParseException;
import java.util.List;

import com.axzhengxin.platform.afterloan.domain.FspCorInf;
import com.axzhengxin.platform.afterloan.mapper.FspCorInfMapper;
import com.axzhengxin.platform.business.domain.BizFactoringInfo;
import com.axzhengxin.platform.business.domain.BizInsuranceInfo;
import com.axzhengxin.platform.business.mapper.BizInsuranceInfoMapper;
import com.axzhengxin.platform.business.service.IBizInsuranceInfoService;
import com.axzhengxin.platform.common.service.IDbExecService;
import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.DictUtils;
import com.ruoyi.common.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 保险信息Service业务层处理
 * 
 * @author axzhengxin
 * @date 2021-04-01
 */
@Slf4j
@Service
public class BizInsuranceInfoServiceImpl implements IBizInsuranceInfoService
{
    @Autowired
    private BizInsuranceInfoMapper bizInsuranceInfoMapper;
    @Autowired
    private FspCorInfMapper fspCorInfMapper;
    @Autowired
    private IDbExecService dbExecService;

    /**
     * 查询保险信息
     * 
     * @param insuranceId 保险信息ID
     * @return 保险信息
     */
    @Override
    public BizInsuranceInfo selectBizInsuranceInfoById(String insuranceId)
    {
        return bizInsuranceInfoMapper.selectBizInsuranceInfoById(insuranceId);
    }

    /**
     * 查询保险信息列表
     * 
     * @param bizInsuranceInfo 保险信息
     * @return 保险信息
     */
    @Override
    @DataScope(deptAlias = "sd",userAlias = "su")
    public List<BizInsuranceInfo> selectBizInsuranceInfoList(BizInsuranceInfo bizInsuranceInfo)
    {
        List<BizInsuranceInfo> bizInsuranceInfos = bizInsuranceInfoMapper.selectBizInsuranceInfoList(bizInsuranceInfo);
        bizInsuranceInfos=buildBizInsuranceInfoList(bizInsuranceInfos);
        return bizInsuranceInfos;
    }

    /**
     * 新增保险信息
     * 
     * @param bizInsuranceInfo 保险信息
     * @return 结果
     */
    @Override
    public int insertBizInsuranceInfo(BizInsuranceInfo bizInsuranceInfo)
    {
        bizInsuranceInfo.setCreateTime(DateUtils.getNowDate());
        return bizInsuranceInfoMapper.insertBizInsuranceInfo(bizInsuranceInfo);
    }

    /**
     * 修改保险信息
     * 
     * @param bizInsuranceInfo 保险信息
     * @return 结果
     */
    @Override
    public int updateBizInsuranceInfo(BizInsuranceInfo bizInsuranceInfo)
    {
        bizInsuranceInfo.setUpdateTime(DateUtils.getNowDate());
        return bizInsuranceInfoMapper.updateBizInsuranceInfo(bizInsuranceInfo);
    }

    /**
     * 批量删除保险信息
     * 
     * @param insuranceIds 需要删除的保险信息ID
     * @return 结果
     */
    @Override
    public int deleteBizInsuranceInfoByIds(String[] insuranceIds)
    {
        return bizInsuranceInfoMapper.deleteBizInsuranceInfoByIds(insuranceIds);
    }

    /**
     * 删除保险信息信息
     * 
     * @param insuranceId 保险信息ID
     * @return 结果
     */
    @Override
    public int deleteBizInsuranceInfoById(String insuranceId)
    {
        return bizInsuranceInfoMapper.deleteBizInsuranceInfoById(insuranceId);
    }

    @Override
    public FspCorInf getFspCorInf(String customerId,String customerName){
        if (StringUtils.isNotBlank(customerId)) {
            FspCorInf fspCorInf = fspCorInfMapper.selectFspCorInfByCustomerId(customerId);
            if (null != fspCorInf) {
                try {
                    dbExecService.getJbxxByEntName(customerName)
                            .ifPresent(result -> {
                                fspCorInf.setEconat(result.getString("ECONAT"));
                                fspCorInf.setEntState(result.getString("STATE"));
                                fspCorInf.setOpScope(result.getString("OPSCOPE"));
                                fspCorInf.setEntTypeName(result.getString("ENTTYPENAME"));
                                try {
                                    fspCorInf.setEstDate(DateUtils.parseDate(result.getString("ESTDATE"), "yyyy-MM-dd HH:mm:ss"));
                                    fspCorInf.setApprDate(DateUtils.parseDate(result.getString("APPRDATE"), "yyyy-MM-dd HH:mm:ss"));
                                } catch (ParseException e) {
                                    log.error("解析日期字符串发生异常", e);
                                }
                            });
                } catch (Exception e) {
                    log.error("远程接口调用发生异常", e);
                    e.printStackTrace();
                }
            }
            return fspCorInf;
        }
        return new FspCorInf();
    }


    public List<BizInsuranceInfo> buildBizInsuranceInfoList(List<BizInsuranceInfo> bizFactoringInfos) {
        bizFactoringInfos.stream().
                forEach(bizFactoringInfo -> {
                    String pciBelongNo = bizFactoringInfo.getPciBelongNo();
                    if (StringUtils.isNotBlank(pciBelongNo)) {
                        String biz_industry_type = DictUtils.getDictLabel("biz_industry_type", pciBelongNo);
                        if (StringUtils.isNotBlank(biz_industry_type)) {

                            bizFactoringInfo.setPciBelongType(biz_industry_type);
                        }
                    }
                    String customerAreaNo = bizFactoringInfo.getCustomerAreaNo();
                    if (StringUtils.isNotBlank(customerAreaNo)) {
                        String area_codes = DictUtils.getDictLabel("area_codes", customerAreaNo);
                        if (StringUtils.isNotBlank(area_codes)) {
                            bizFactoringInfo.setCustomerArea(area_codes);

                        }
                    }
                });
        return bizFactoringInfos;
    }
}
